package com.rj.core.validation;

import com.rj.core.validation.constraints.In;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;

import java.util.Arrays;

/**
 * @author YFX_RANLUBO
 */
public class InValidator implements ConstraintValidator<In, String> {
    protected String[] inValue;

    public InValidator() {
    }

    @Override
    public void initialize(In constraintAnnotation) {
        inValue = constraintAnnotation.value();
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) {
        if (value == null) {
            return false;
        }
        return Arrays.stream(inValue).anyMatch(v -> value.equals(v));
    }


}